package com.flycms.web.app;

import com.flycms.core.entity.PageVo;
import com.flycms.core.entity.RestDataVo;
import com.flycms.module.app.enums.AppUpdateType;
import com.flycms.module.app.model.App;
import com.flycms.module.app.param.AppQueryParam;
import com.flycms.module.app.service.AppService;
import com.flycms.module.comment.service.CommentsService;
import com.flycms.utils.ConvertUtils;
import com.flycms.web.app.vo.AppVO;
import com.flycms.web.app.vo.HotAppVO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author modenglang
 */

@RestController
@Slf4j
public class AppController {

    @Autowired
    private AppService appService;
    @Autowired
    private CommentsService commentsService;

    /**
     * 查找APP最热排行帮
     *
     * @return 返回结果 查询不到返回空集合
     */
    @GetMapping("/app/list/hot")
    public RestDataVo getHotAppList() {
        List<App> apps = appService.getHotAppList();
        if (CollectionUtils.isEmpty(apps)) {
            return RestDataVo.OK(Lists.newArrayList());
        }
        List<HotAppVO> hotAppVOS = ConvertUtils.sourceToTarget(apps, HotAppVO.class);
        return RestDataVo.OK(hotAppVOS);
    }

    /**
     * 根据入参查询APP列表
     *
     * @param param 入参
     * @return 返回列表
     */
    @GetMapping(value = {"/app/list", "/system/app/list"})
    public RestDataVo getAppList(AppQueryParam param) {
        if (this.checkParam(param)) {
            return RestDataVo.ERROR("只能选择一种排序");
        }
        PageVo<App> appPage = appService.getAppPage(param);
        if (CollectionUtils.isEmpty(appPage.getList())) {
            return RestDataVo.PAGE(appPage.getList(), appPage.getPageNum(), param.getPageSize(), 0);
        }
        List<AppVO> appVOS = ConvertUtils.sourceToTarget(appPage.getList(), AppVO.class);
        return RestDataVo.PAGE(appVOS, appPage.getPageNum(), param.getPageSize(), appPage.getCount());
    }

    private boolean checkParam(AppQueryParam param) {
        int sum = 0;
        if (Objects.nonNull(param.getIntegratedDesc())) {
            sum++;
        }
        if (Objects.nonNull(param.getCreatedTimeDesc())) {
            sum++;
        }
        if (Objects.nonNull(param.getUpdatedTimeDesc())) {
            sum++;
        }
        if (Objects.nonNull(param.getEvalDesc())) {
            sum++;
        }
        return sum > 1;
    }

    /**
     * 根据APP ID查询APP详情
     *
     * @param id ID
     * @return APP详情
     */
    @GetMapping("/app/{id}")
    public RestDataVo getActivityList(@PathVariable("id") Long id) {
        App app = appService.getAppDetailById(id);
        if (Objects.isNull(app)) {
            return RestDataVo.OK(null);
        }
        //获取星星
        BigDecimal star = commentsService.getStarByAppId(id);
        app.setStar(star);
        //新增点击数
        appService.updateCount(AppUpdateType.HIT.getCode(), id);
        return RestDataVo.OK(app);
    }

    /**
     * 新增app
     *
     * @param app app 数据
     * @return 新增结果
     */
    @PostMapping("/system/app/save")
    public RestDataVo save(@RequestBody App app) {
        try {
            appService.save(app);
        } catch (Exception e) {
            log.error("新增app异常:", e);
            return RestDataVo.ERROR("新增app异常");
        }
        return RestDataVo.OK("新增成功！");
    }

    /**
     * 修改app
     *
     * @param app app 数据
     * @return 修改结果
     */
    @PutMapping("/system/app/update")
    public RestDataVo update(@RequestBody App app) {
        try {
            appService.update(app);
        } catch (Exception e) {
            log.error("修改app异常:", e);
            return RestDataVo.ERROR("修改app异常");
        }
        return RestDataVo.OK("修改成功！");
    }

    /**
     * 删除app
     *
     * @return 删除结果
     */
    @DeleteMapping("/system/app/del/{id}")
    public RestDataVo update(@PathVariable("id") Long id) {
        try {
            appService.del(id);
        } catch (Exception e) {
            log.error("删除app异常:", e);
            return RestDataVo.ERROR("删除app异常");
        }
        return RestDataVo.OK("删除成功！");
    }

    /**
     * 查询APP详情
     */
    @GetMapping("/system/app/{id}")
    public RestDataVo getApp(@PathVariable("id") Long id) {
        App app = appService.getAppDetailById(id);
        if (Objects.isNull(app)) {
            return RestDataVo.OK(null);
        }
        return RestDataVo.OK(app);
    }

}
